﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Cricket.LinqModel;
using Cricket.LinqData;


public partial class _Default : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    HttpCookie cookie;
    decimal countTotal1 = 0;
    decimal countTotal2 = 0;
    decimal countTotal3 = 0;
    decimal countTotal4 = 0;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        cookie = Request.Cookies["Client"];
        if (!IsPostBack)
        {
            if (cookie == null)
                Response.Redirect("Login.aspx");
            else
                bindMatch();
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchesForReportResult> lstResult = new List<GetMatchesForReportResult>();
        lstResult = matchSchedule.GetMatchesForReport();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    private static DataTable GetData(string query)
    {
        string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Client"];
        if (cookie == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            grdAllCustomers.ToolTip = Convert.ToString(cookie["ClientId"]);
            matchSchedule = new MatchSchedule(_dataObject);
            List<GetTodayMatcheForClientResult> lstResult = new List<GetTodayMatcheForClientResult>();
            lstResult = matchSchedule.GetTodayMatchesForClient();

            matchSchedule = new MatchSchedule(_dataObject);
            List<GetTeamNameByMatchDetailResult> lst1 = new List<GetTeamNameByMatchDetailResult>();
            lst1 = matchSchedule.GetTeamNameByMatchDetail(Convert.ToInt32(drpMatch.SelectedValue));
            if (lst1.Count > 0)
            {
                grdAllCustomers.Columns[3].HeaderText = lst1[0].Team1Name;
                grdAllCustomers.Columns[4].HeaderText = lst1[0].Team2Name;
                //grdAllCustomers.Columns[3].HeaderText = "Team 1";
                //grdAllCustomers.Columns[4].HeaderText = "Team 2";
            }
            else
            {
                grdAllCustomers.Columns[3].HeaderText = "Team 1";
                grdAllCustomers.Columns[4].HeaderText = "Team 2";
            }

            grdAllCustomers.DataSource = GetData(string.Format("SELECT CASE WHEN B.IsDraw = 1 THEN CASE CB.BetFor WHEN 'L' THEN B.DrawLagadi ELSE B.DrawKhadhi END WHEN B.IsTie = 1 THEN CASE CB.BetFor WHEN 'L' THEN B.TieLagadi ELSE B.TieKhadhi END ELSE	CASE CB.BetFor WHEN 'L' THEN B.LagadiRate ELSE B.KhadhiRate END END AS Rate,CB.BetFor AS BettingFor,BettingAmount AS BettingAmount, TeamAAmount AS TeamAAmount,TeamBAmount AS TeamBAmount, TieAmount AS TieAmount, DrawAmount AS DrawAmount,T.Abbrivation As TeamName,CONVERT(varchar(20),DATEADD(MI,30,DATEADD(hh,5,CB.CreatedDate)),113) As Date from Client C INNER JOIN CustomerBetting CB ON C.Id = CB.CreatedBy INNER JOIN Betting B ON B.Id = CB.BettingId INNER JOIN Team T ON B.TeamId = T.Id WHERE C.Id = '{0}' AND (B.AdminId = " + Convert.ToInt32(cookie["AdminId"]) + " OR B.AdminId = 3) AND CB.MatchSchduleId = " + drpMatch.SelectedValue + "  AND B.BettingTypeId = 2 ", Convert.ToInt32(cookie["ClientId"])));
            grdAllCustomers.DataBind();
        }
    }
    protected void grdAllCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchDetailsResult> lst = new List<GetMatchDetailsResult>();
        lst = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
        if (lst.Count > 0)
        {
            if (lst[0].MatchType.ToLower() == "test")
            {
                grdAllCustomers.Columns[6].Visible = true;
            }
            else
            {
                grdAllCustomers.Columns[6].Visible = false;
            }

        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblBettinAmount = (Label)e.Row.FindControl("lblBettinAmount");
            if (lblBettinAmount.Text != "")
            {
                countTotal1 = countTotal1 + Convert.ToDecimal(lblBettinAmount.Text);
            }
            Label lblTeamAAmount = (Label)e.Row.FindControl("lblTeamAAmount");
            if (lblTeamAAmount.Text != "")
            {
                countTotal2 = countTotal2 + Convert.ToDecimal(lblTeamAAmount.Text);
            }
            Label lblTeamBAmount = (Label)e.Row.FindControl("lblTeamBAmount");
            if (lblTeamBAmount.Text != "")
            {
                countTotal3 = countTotal3 + Convert.ToDecimal(lblTeamBAmount.Text);
            }
            Label lblDrawAmount = (Label)e.Row.FindControl("lblDrawAmount");
            if (lblDrawAmount.Text != "")
            {
                countTotal4 = countTotal4 + Convert.ToDecimal(lblDrawAmount.Text);
            }

        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblGrandBettinAmount = (Label)e.Row.FindControl("lblGrandBettinAmount");
            lblGrandBettinAmount.Text = countTotal1.ToString();

            Label lblGrandTeamAAmount = (Label)e.Row.FindControl("lblGrandTeamAAmount");
            lblGrandTeamAAmount.Text = countTotal2.ToString();

            Label lblGrandTeamBAmount = (Label)e.Row.FindControl("lblGrandTeamBAmount");
            lblGrandTeamBAmount.Text = countTotal3.ToString();

            Label lblGrandDrawAmount = (Label)e.Row.FindControl("lblGrandDrawAmount");
            lblGrandDrawAmount.Text = countTotal4.ToString();

        }
    }


}